"""
官方文档
https://www.gradio.app/guides
"""


import gradio as gr

def greet(name):
    return "Hello " + name + "!!"



def test1():
    """hello world,  gr.Interface,  左边输入 右边输出"""
    demo = gr.Interface(fn=greet, inputs="text", outputs="text")

    demo.launch(server_name='0.0.0.0')



import random

def random_response(message, history):
    return random.choice(["Yes", "No"])


def test2():
    """测试对话界面"""
    gr.ChatInterface(random_response).launch()




import time

def slow_echo(message, history):
    for i in range(len(message)):
        time.sleep(0.3)
        yield "You typed: " + message[: i+1]

def test3():
    """流式对话"""
    gr.ChatInterface(slow_echo).launch()


def yes_man(message, history):
    if message.endswith("?"):
        return "Yes"
    else:
        return "Ask me anything!"

def test4():
    """使用更多参数, 自定义样式"""
    gr.ChatInterface(
        yes_man,
        chatbot=gr.Chatbot(height=300),
        textbox=gr.Textbox(placeholder="Ask me a yes or no question", container=False, scale=7),
        title="Yes Man",
        description="Ask Yes Man any question",
        theme="soft",
        examples=["Hello", "Am I cool?", "Are tomatoes vegetables?"],
        cache_examples=True,
        retry_btn=None,
        undo_btn="Delete Previous",
        clear_btn="Clear",
    ).launch()




def test_block1():
    """gr.Blocks 自定义对话框组件"""

    with gr.Blocks() as demo:
        chatbot = gr.Chatbot()
        msg = gr.Textbox()
        clear = gr.ClearButton([msg, chatbot])

        def respond(message, chat_history):
            bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
            chat_history.append((message, bot_message))
            time.sleep(2)
            return "", chat_history

        msg.submit(respond, [msg, chatbot], [msg, chatbot])

    demo.launch()



def test_interface1():
    """测试 gr.Interface: 处理函数的参数 和输入控件对应 """
    def greet(name, intensity):
        return "Hello, " + name + "!" * int(intensity)

    demo = gr.Interface(
        fn=greet,
        inputs=["text", "slider"],
        outputs=["text"],
    )

    demo.launch()








if __name__ == '__main__':
    test_interface1()